/*
 * Class name: Player.java
 * Version: 1.0
 * Date: 9.10. 2013
 */
package cz.muni.fi.pa165.soccergamesarchive.persistencelayer.entities;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

/**
 *
 * @author Michal Ďuriš
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "Player.getAllPlayers",
                query = "SELECT p FROM Player p"),
    @NamedQuery(name = "Player.getPlayerDetail",
                query = "SELECT "
                + "new cz.muni.fi.pa165.soccergamesarchive.businesslayer.dtos.PlayerDetailDTO("
                + "p.firstName, "
                + "p.lastName, "
                + "p.team.name, "
                + "p.shirtNumber, "
                + "(SELECT count(g) "
                + "   FROM Goal g "
                + "  WHERE g.player.id = p.id )) "
                + "FROM Player p "
                + "WHERE p.id = :playerID "
    ),
    @NamedQuery(name = "Player.findPlayer",
                query = "SELECT p "
                + "FROM Player p "
                + "WHERE LOWER(p.firstName) LIKE :firstName "
                + "AND LOWER(p.lastName) LIKE :lastName ")
})
public class Player implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(length = 40, nullable = false)
    private String firstName;

    @Column(length = 40, nullable = false)
    private String lastName;

    @Column(length = 2, nullable = false)
    private int shirtNumber = 0;

    @ManyToOne
    @JoinColumn(nullable = false)
    private Team team;

    public Player() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getShirtNumber() {
        return shirtNumber;
    }

    public void setShirtNumber(int shirtNumber) {
        this.shirtNumber = shirtNumber;
    }

    public Team getTeam() {
        return team;
    }

    public void setTeam(Team team) {
        this.team = team;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {

        if (!(object instanceof Player)) {
            return false;
        }

        Player other = (Player) object;

        if (this.id == null && other.id == null) {
            if (!isEqual(this.firstName, other.firstName)) {
                return false;
            }

            if (!isEqual(this.lastName, other.lastName)) {
                return false;
            }

            if (this.shirtNumber != other.shirtNumber) {
                return false;
            }

            if (!isEqual(this.team, other.team)) {
                return false;
            }
        }

        return (this.id != null || other.id == null) && (this.id == null
                || this.id.equals(
                        other.id));
    }

    private static boolean isEqual(Object expected, Object actual) {
        if ((expected == null && actual != null) || (expected != null && actual
                == null)) {
            return false;
        }

        return expected == null || expected.equals(actual);
    }

    @Override
    public String toString() {
        return "cz.muni.fi.pa165.soccergamesarchive.persistencelayer.entities.Player[ id="
                + id + " ]";
    }

}
